<template>
  <div class="list">
    <!-- 头部 -->
    <common-head v-show="showHead" title="搜索结果" />
    <!-- 排序规则 -->
    <div class="filter" :style="{top:navTop}">
      <div @click="orderBySyn"
       :class="['synthesize','order' ,{
        active: order.by === 'synthesize'
        }]">
        <span>综合</span>
        <div :class="['down', 'angle',{
        active: order.by ==='synthesize'&&order.upDown===1
        }]"></div>
      </div>
      <div  @click="orderBySale"
      :class="['sale','order' ,{
        active: order.by === 'sale'
        }]">
        <span>销量</span>
        <div :class="['up' ,'angle',{
          active:order.by === 'sale'&&order.upDown===0
        }]"></div>
        <div :class="['down' ,'angle',{
          active:order.by === 'sale'&&order.upDown===1
        }]"></div>
      </div>
      <div @click="orderByPrice"
       :class="['price','order' ,{
        active: order.by === 'price'
        }]">
        <span>价格</span>
        <div :class="['up' ,'angle',{
          active:order.by === 'price'&&order.upDown===0
        }]"></div>
        <div :class="['down' ,'angle',{
          active:order.by === 'price'&&order.upDown===1
        }]"></div>
      </div>
    </div>
      <div class="warp">
        <van-empty description="暂无商品" v-if="!items.length" />
          <div class="items">
      <div class="item" @click="enterDetail(item.id)" v-for="item in items" :key="item.id">
        <div class="item-left">
          <img :src="item.pic" alt />
        </div>
        <div class="item-right">
          <h3>{{ item.name }}</h3>
          <p>
              {{ item.characteristic ? item.characteristic: '暂无描述' | filterDesc}}
          </p>
          <div class="pic">
            <div class="pic-left">
              <h3>
                  {{ item.minPrice | currency }}
              </h3>
              <span>已销售13.6万</span>
            </div>
            <div class="pic-right">
              <van-icon name="cart-circle" />
            </div>
          </div>
        </div>
      </div>
    </div>
      </div>
    <tabbar />
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import Tabbar from '@components/tabbar'
import { fetchHomeList } from '@api'
export default {
  components: {
    CommonHead,
    Tabbar
  },
  data () {
    return {
      order: {
        by: 'synthesize', // synthesize按照综合排序 sale 按照销量排序 price 按照价格排序
        upDown: 1 // 1降序 0 升序
      },
      showHead: true,
      navTop: '46px',
      items: []
    }
  },
  mounted () {
    // 在dom加载完毕调用滚动
    this.scrollHead()
  },
  created () {
    // 列表初始请求数据调用
    this.fetchList()
  },
  methods: {
    // 点击进入详情页
    enterDetail (id) {
      console.log(1111)
      this.$router.push(`/itemDetail/${id}`)
    },
    // 请求列表数据
    fetchList () {
      fetchHomeList({
        // 传参获取路由跳转过来的参数 传给后端 和排序的方式
        k: this.$route.query.k,
        orderBy: this.orderBy
      }).then(res => {
        this.items = []
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    },
    // 滚动高度到达一定值 头部隐藏
    scrollHead () {
      window.onscroll = () => {
        const sTop = document.documentElement.scrollTop || document.body.scrollTop
        if (sTop > 80) {
          // 大于80隐藏
          this.showHead = false
          // 过滤商品的上去
          this.navTop = 0
        } else {
          this.showHead = true
          this.navTop = '46px'
        }
      }
    },
    orderBySyn () {
      // 点击按照综合
      // 判断如果此时是按照价格或者是销量排序的话 把他切换为综合
      if (this.order.by !== 'synthesize') {
        this.order.by = 'synthesize'
        this.order.upDown = 1
      }
    },
    orderBySale () {
      // 点击按照销量 如果此时不是销量 先把他切换过来 默认是降序
      if (this.order.by !== 'sale') {
        this.order.by = 'sale'
        this.order.upDown = 1
      } else {
        // 如果此时是销量的话 那就升序变降序 降序变升序
        if (this.order.upDown === 1) {
          this.order.upDown = 0
        } else {
          this.order.upDown = 1
        }
      }
    },
    orderByPrice () {
      // 点击按照价格 如果此时不是按照价格的话 先把他切换过来 默认是降序
      if (this.order.by !== 'price') {
        this.order.by = 'price'
        this.order.upDown = 1
      } else {
        // 如果此时已经是价格了 那就吧他升序变降序 降序变升序
        if (this.order.upDown === 1) {
          this.order.upDown = 0
        } else {
          this.order.upDown = 1
        }
      }
    }
  },
  filters: {
    filterDesc (v, len = 10) {
      if (v.length > 10) {
        return v.substr(0, len) + '...'
      } else {
        return v
      }
    },
    currency (v, sign = '￥') {
      return sign + v
    }
  },
  watch: {
    // 深度监听order的变化 如果变化了 那就立即在从新调用请求函数
    order: {
      handler (val) {
        console.log('1111')
        this.fetchList()
      },
      deep: true
    }
  },
  computed: {
    orderBy () {
      if (this.order.by === 'sale' && this.order.upDown === 1) {
        return 'ordersDown'
      }
      if (this.order.by === 'sale' && this.order.upDown === 0) {
        return 'ordersUp'
      }
      if (this.order.by === 'price' && this.order.upDown === 0) {
        return 'priceUp'
      }
      if (this.order.by === 'price' && this.order.upDown === 1) {
        return 'priceDown'
      }
      return 'ordersDown'
    }
  }

}
</script>

<style lang="scss" scoped>
.list{
  background-color: #fff;
  .filter{
    height: 40px;
    width: 100%;
    position: fixed;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: space-around;
    border-bottom:1px solid #ebedf0;
    background-color: #fff;
    .order{
      font-size: 13px;
      color: #333;
      position: relative;
      &.active{
        color: #FF734C;
      }
      .angle{
        width: 0;
        height: 0;
        border: 4px solid transparent;
        position: absolute;
        right: -12px;
      }
      & .up{
        border-bottom-color:#333;
        top: -1px;
        &.active{
           border-bottom-color:#FF734C;
        }
      }
       & .down{
        border-top-color:#333;
        top: 8px;
        &.active{
           border-top-color:#FF734C;
        }
      }
    }
  }
  .warp{
  padding: 97px 0 50px 0;
  background-color: #fff;
  .item {
  background-color: #fff;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  margin-bottom: 20px;
  .item-left {
    img {
      width: 170px;
      height: 186px;
    }
  }
  .item-right{
    width: 100%;
    box-sizing: border-box;
    padding: 12px 0 8px 12px;
    h3{
      font-weight: 600;
    }
    p{
      font-size: 13px;
      line-height: 18px;
    }
    .pic{
      margin-top: 80px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-right: 10px;
      box-sizing: border-box;
      .pic-left{
        span{
          font-size: 12px;
          color: #71797f;
        }
      }
      .pic-right{
        font-size: 22px;
        color: rgb(255, 130, 100);
      }
    }
  }
}
}
}

</style>
